package com.roseski.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.roseski.dao.RoseskiPayOrderMapper;
import com.roseski.model.RoseskiPayOrder;
import com.roseski.util.PayCommonUtil;
import com.roseski.wechatutil.MyConfig;
import com.roseski.wechatutil.WXPay;
import com.roseski.wechatutil.WXPayConstants;
import com.roseski.wechatutil.WXPayUtil;

@Service
public class WechatPayServiceImpl {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	RoseskiPayOrderMapper roseskiPayOrderMapper;
	@Value("${appId}")
	private String appId;
	@Value("${merchantId}")
	private String merchantId;
	@Value("${key}")
	private String key;
	@Value("${notify_url}")
	private String notifyUrl;
	@Autowired
	MyConfig config;
	
	
	public Map<String,String> createOrder(RoseskiPayOrder payOrder, String openId) throws Exception{
		//微信下单
		WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "roseski");
        data.put("out_trade_no", payOrder.getPayOrderNo());
        data.put("fee_type", "CNY");
        data.put("total_fee", payOrder.getMoney().toString());
        data.put("spbill_create_ip", "127.0.0.1");
        data.put("notify_url", notifyUrl);
        data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
        data.put("openid", openId);  // 此处指定为扫码支付
        data.put("product_id", "12");
        logger.info("微信支付请求参数：{}", data);
        Map<String, String> resp = wxpay.unifiedOrder(data);
        logger.info("微信支付响应参数：{}", resp);
        if("SUCCESS".equals(resp.get("return_code"))) {
        	//SortedMap<Object, Object> params = new TreeMap<Object, Object>();
        	Map<String, String> params = new HashMap<String, String>();
        	params.put("appId", appId);
        	params.put("timeStamp", Long.toString(new Date().getTime()));
        	params.put("nonceStr", PayCommonUtil.CreateNoncestr(32));
        	params.put("package", "prepay_id=" + resp.get("prepay_id"));
        	params.put("signType", "HMAC-SHA256");
        	//String paySign = PayCommonUtil.createSign("UTF-8", params, key);
        	String paySign = WXPayUtil.generateSignature(params, key, WXPayConstants.SignType.HMACSHA256);
        	params.put("paySign", paySign);
        	params.put("orderId", payOrder.getOrderNo());
        	logger.info("微信支付调起收银台信息：{}", params);
        	return params;
        }else {
        	RoseskiPayOrder updateParams = new RoseskiPayOrder();
        	updateParams.setPayOrderNo(payOrder.getPayOrderNo());
        	updateParams.setFailCode(resp.get("result_code"));
        	updateParams.setFailMsg(resp.get("return_msg"));
        	roseskiPayOrderMapper.updateByPrimaryKeySelective(updateParams);
        	//更新支付单错误信息
        	throw new RuntimeException(resp.get("return_code") + resp.get("return_msg"));
        }
	}

}
